const context = {

  handlePlayList(m3u8List = [], vod_name = '电影名称') {
    let list = new Array()
    for (const i of m3u8List) {
      const j = i.split('$')
      let rowIndex = list.length + 1
      let label = m3u8List.length > 1 ? `第${rowIndex.toString().padStart(4,0)}集` : vod_name
      if (j.length > 1) {
        for (let m = 0; m < j.length; m++) {
          if (j[m].indexOf('.m3u8') >= 0 && j[m].startsWith('http')) {
            let d = {
              index: i,
              value: j[m],
              label: label
            }
            list.push(d)
            break
          }
        }
      } else {
        let d = {
          index: i,
          value: j[0],
          label: label
        }
        list.push(d)
      }
    }
    return this.handleGroupPlayList(list)
    // console.log(list)
    // return list
  },
  handleGroupPlayList(playList = [], pageSize = 40) {
    let result = {
      group: false,
      data: [],
      pageCount: 1
    }

    if (playList.length < pageSize) {
      result.data = playList
      return result
    }

    let pageCount = Math.ceil(playList.length / pageSize)
    let list = new Array()
    for (let i = 0; i < pageCount; i++) {
      let start = i * pageSize
      let end = (i + 1) * pageSize
      let item = {
        label: `${start+1} - ${end}`,
        start: start + 1,
        end,
        children: []
      }
      let j = start
      for (; j < end && j < playList.length; j++) {
        let obj = {
          ...playList[j]
        }
        item.children.push(obj)
      }
      item.end = j
      item.label = `${item.start.toString().padStart(4,0)} - ${item.end.toString().padStart(4,0)}`
      list.push(item)
    }
    result.data = list
    result.group = true
    result.pageCount = pageCount
    // console.log(result)
    return result
  },
  parseIPTV(contents = '') {
    let list = new Array()
    let rows = contents.split('\n')
    let fnTitle = (text = '') => {
      //解析地址 
      let pops = text.split(' ')
      let name = pops[1].match(/".+\"/)
      if (name) {
        //名称解析错误
        name = name[0].replace(/"/g, '')
      }
      let logo = pops[2].match(/".+\"/)
      if (logo) {
        //名称解析错误
        logo = logo[0].replace(/"/g, '')
      }
      // console.log(logo) 
      let item = {
        id: pops[0],
        name: name,
        face: logo,
        url: ''
      }
      return item
    }
    for (var i = 0; i < rows.length; i++) {
      let row = rows[i]
      if (row.startsWith('#EXTINF')) {
        //解析名称
        let item = fnTitle(row)
        item.url = rows[i + 1]
        i++
        list.push(item)
      }
    }
    // console.log(list)
    return list
  },
  handleCku(data) {
		// console.log(data) 
    let keys = ['analyze', 'channel', 'drive', 'iptv', 'sites']
    let objKeys = Object.keys(data)
    let result = {
      analyze: [],
      channel: [],
      drive: [],
      iptv: [],
      sites: [],
    }
		if(Array.isArray(data)){
			result.sites = data
			return result
		}
		
    keys.forEach(t => {
      if (objKeys.includes(t)) {
        let val = data[t]
        if (Array.isArray(val)) {
          result[t] = val
        }
      }
    })
    return result
  }
}



export default {
  ...context
}